<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 2020/12/10
 * Time: 15:31
 */

namespace app\common\module;
class Wechat
{

    //发送接单小程序通知给普通用户
    public function sendOrderMsg($openid,$orderNumber,$type_name,$true_name,$phone){
        $smallApp = [
            'touser' => $openid,
            'template_id' => 'lJp9bid7nvrUz1dVqKwuPCx8HW1hvGakzJG_4f4Nm9k',
            "page" => "pages/order/index?id=1",
            "miniprogram_state" => config('public.miniprogram_state'),
            "form_id" => "745",
            "data" => [
                "character_string1" => [
                    "value" => $orderNumber
                ],
                "thing2" => [
                    "value" => $type_name
                ],
                "name3" => [
                    "value" =>$true_name
                ],
                "phone_number6" => [
                    "value" => $phone
                ],
                "time10" => [
                    "value" => date("Y-m-d H:i:s")
                ],
            ]
        ];
        $result = $this->sendMiniMsg($smallApp);
        if($result['errcode'] ==43101){
            return ['code'=>$result['errcode'],'msg'=>"重复发送"];
        }
        return ['errcode'=>$result,'msg'=>$result['errmsg']];
    }
//发送审核结果
    public function sendResultMsg($openid,$status,$project,$beizhu){
        $smallApp = [
            'touser' => $openid,
            'template_id' => 'J9p2S3kKzrxzDuT67f0PVMFb7Voo5ma0mG9dbRTePOM',
            "page" => "pages/user/recruit/index",
            "miniprogram_state" =>config('public.miniprogram_state'),
            "form_id" => "5933",
            "data" => [
                "phrase2" => [
                    "value" => $status
                ],
                "thing6" => [
                    "value" => $project
                ],
                "thing3" => [
                    "value" =>$beizhu
                ],
                "time4" => [
                    "value" => date("Y-m-d H:i:s")
                ],
            ]
        ];
        $result = $this->sendMiniMsg($smallApp);
        if($result['errcode'] ==43101){
            return ['code'=>$result['errcode'],'msg'=>"重复发送"];
        }
        return ['errcode'=>$result,'msg'=>$result['errmsg']];
    }

    //通知回收员接单
    public function sendReceiveMsg($openid,$time,$address,$true_name,$phone){
        $wechat = new Wechat();
        $token =   $wechat->getPublicAccess(true);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        $template_id = '0HVG45SRNcsAdsyZtniAo297ITVuNKd51oypeheEGa0';
        $data = [
            'touser'=>$openid,//openid,
            'template_id'=>$template_id,
            "url"=>"http://weixin.qq.com/download",
            "miniprogram"=>[
                "appid"=>config('public.wx_appid'),
                "pagepath"=>"pages/user/admin/index",
            ],
            "data"=>[
                "first"=>[
                    "value"=>"有新的回收订单,快来接单吧"
                ],
                "keyword1"=>[
                    "value"=>$time
                ],
                "keyword2"=>[
                    "value"=>$address
                ],
                "keyword3"=>[
                    "value"=>$true_name
                ],
                "keyword4"=>[
                    "value"=>$phone
                ],
                "keyword5"=>[
                    "value"=>date("Y-m-d H:i:s")
                ],
                "remark"=>[
                    "value"=>"快去接单吧~!"
                ]

            ]
        ];
        return curl_post($url,json_encode($data,JSON_UNESCAPED_UNICODE));

    }

    //发送小程序模板消息
    private function sendMiniMsg($smallApp,$is_shua=false){
        $AccessToken =  $this->getAccessToken($is_shua);
        $url ='https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$AccessToken;
        $result = curl_post($url,json_encode($smallApp,JSON_UNESCAPED_UNICODE));
        $result = json_decode($result,true);
        if($result['errcode'] ==40001){
            return $this->sendMiniMsg($smallApp,true);
        }
        return $result;
    }

    public function getAccessToken($is_shuawxin=false){
        $cache = cache('access_token');
        if($cache && !$is_shuawxin){
            if($cache['expires_time'] > time()){
                return $cache['access_token'];
            }
        }
        $url= 'https://api.weixin.qq.com/cgi-bin/token';
        $data = [
            'grant_type'=>"client_credential",
            'appid'=>config('public.wx_appid'),
            'secret'=>config('public.wx_secret')
        ];
        $url.='?'.http_build_query($data);
        $result = curl_get($url);
        cache('access_token',[
            'expires_time'=>time()+$result['expires_in'],
            'access_token'=>$result['access_token']
        ]);
        return $result['access_token'];
    }
    public function getPublicAccess($is_shuawxin = false){
        $cache = cache('public_access_token');
        if($cache && !$is_shuawxin){
            if($cache['expires_time'] > time()){
                return $cache['access_token'];
            }
        }
        $url= 'https://api.weixin.qq.com/cgi-bin/token';
        $data = [
            'grant_type'=>"client_credential",
            'appid'=>config('public.mp_appid'),
            'secret'=>config('public.mp_secret')
        ];
        $url.='?'.http_build_query($data);
        $result = curl_get($url);
        cache('public_access_token',[
            'expires_time'=>time()+$result['expires_in'],
            'access_token'=>$result['access_token']
        ]);
        return $result['access_token'];
    }
    public function PublicUnionId($openId){
        $access_token = $this->getPublicAccess();
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openId."&lang=zh_CN";
        return  $result = curl_get($url);
    }
}